package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.RegisterService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.spi.RegisterableService;

@Controller
@RequestMapping("registerController")
public class RegisterController {

    @DubboReference
    private RegisterService registerService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("findUser")
    @ResponseBody
    public AjaxResult findUser(String username){
        try {
            int i = registerService.findUser(username);
            return AjaxResult.OK().setData(i);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //借助中间件,发送短信
    @RequestMapping("texting")
    @ResponseBody
    public AjaxResult texting(String phone){
        if (phone == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            rocketMQTemplate.convertAndSend("sms-topic",phone);
            return AjaxResult.OK();
        } catch (MessagingException e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //注册功能
    @RequestMapping("register")
    @ResponseBody
    public AjaxResult register(TbUser user,String code){
        if (user == null && code == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            Integer i = registerService.register(user,code);
            return AjaxResult.OK().setData(i);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
